﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Photography.App_Data;

namespace Photography.Admin
{
    public partial class AddContact : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!(Master as Photography.Admin.AdminMaster).AdminID.HasValue)
                Response.Redirect("Default.aspx");

            if (!IsPostBack && !string.IsNullOrEmpty(Request["ContactID"]))
            {
                Photography.App_Data.PhotographyDataContext context = new Photography.App_Data.PhotographyDataContext();

                GetContactsResult contact = (from a in context.GetContacts() where a.ID == int.Parse(Request["ContactID"]) select a).First();
                txtName.Text = contact.Name;
                txtEmail.Text = contact.Email;
                txtMobile.Text = contact.Mobile;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Photography.App_Data.PhotographyDataContext context = new Photography.App_Data.PhotographyDataContext();

            if (string.IsNullOrEmpty(Request["ContactID"]))
            {
                context.AddContact(txtName.Text, txtEmail.Text, txtMobile.Text);
            }
            else
            {
                context.UpdateContact(int.Parse(Request["ContactID"]), txtName.Text, txtEmail.Text, txtMobile.Text);
            }

            Response.Redirect("SendEmail.aspx");
        }
    }
}
